package main

import (
	base "2024-fall-etohlcin3-backend/cmd/kitex_gen/industics/blockchain/base"
	"context"

	"github.com/hyperledger/fabric-gateway/pkg/client"
)

// PlanServiceImpl implements the last service interface defined in the IDL.
type PlanServiceImpl struct{}

var PlanOrg *client.Contract

// GetProductionPlan implements the PlanServiceImpl interface.
func (s *PlanServiceImpl) GetProductionPlan(ctx context.Context, req *base.BaseGetReq) (resp *base.BaseGetResp, err error) {
	evaluateResult, err := readForKey(req.GetId())

	if err != nil {
		resp = &base.BaseGetResp{
			Code:     "404",
			Msg:      "not exist",
			JsonData: err.Error(),
		}
		return
	}

	resp = &base.BaseGetResp{
		Code:     "200",
		Msg:      "ok",
		JsonData: string(evaluateResult),
	}
	return
}

// GetProductionPlanReport implements the PlanServiceImpl interface.
func (s *PlanServiceImpl) GetProductionPlanReport(ctx context.Context, req *base.BaseGetReq) (resp *base.BaseGetResp, err error) {
	evaluateResult, err := readForKey(req.GetId())

	if err != nil {
		resp = &base.BaseGetResp{
			Code:     "404",
			Msg:      "not exist",
			JsonData: err.Error(),
		}
		return
	}

	resp = &base.BaseGetResp{
		Code:     "200",
		Msg:      "ok",
		JsonData: string(evaluateResult),
	}
	return
}

// GetProductionPlanHistory implements the PlanServiceImpl interface.
func (s *PlanServiceImpl) GetProductionPlanHistory(ctx context.Context, req *base.BaseGetReq) (resp *base.BaseGetResp, err error) {
	evaluateResult, err := readHistoryForKey(req.GetId())
	if err != nil {
		return nil, err
	}
	resp = &base.BaseGetResp{
		Code:     "200",
		Msg:      "ok",
		JsonData: string(evaluateResult),
	}
	return
}

// GetProductionPlanReportHistory implements the PlanServiceImpl interface.
func (s *PlanServiceImpl) GetProductionPlanReportHistory(ctx context.Context, req *base.BaseGetReq) (resp *base.BaseGetResp, err error) {
	evaluateResult, err := readHistoryForKey(req.GetId())
	if err != nil {
		return nil, err
	}
	resp = &base.BaseGetResp{
		Code:     "200",
		Msg:      "ok",
		JsonData: string(evaluateResult),
	}
	return
}

// GetProductionPlanByRange implements the PlanServiceImpl interface.
func (s *PlanServiceImpl) GetProductionPlanByRange(ctx context.Context, req *base.BaseRangeGetReq) (resp *base.BaseGetResp, err error) {
	evaluateResult, err := readForRange(req.GetRangeStart(), req.GetRangeEnd())
	if err != nil {
		return nil, err
	}
	resp = &base.BaseGetResp{
		Code:     "200",
		Msg:      "ok",
		JsonData: string(evaluateResult),
	}
	return
}

// GetProductionPlanReportByRange implements the PlanServiceImpl interface.
func (s *PlanServiceImpl) GetProductionPlanReportByRange(ctx context.Context, req *base.BaseRangeGetReq) (resp *base.BaseGetResp, err error) {
	evaluateResult, err := readForRange(req.GetRangeStart(), req.GetRangeEnd())
	if err != nil {
		return nil, err
	}
	resp = &base.BaseGetResp{
		Code:     "200",
		Msg:      "ok",
		JsonData: string(evaluateResult),
	}
	return
}

// CreateProductionPlan implements the PlanServiceImpl interface.
func (s *PlanServiceImpl) CreateProductionPlan(ctx context.Context, req *base.BasePostReq) (resp *base.BasePostResp, err error) {
	_, err = createForKey(req.GetId(), req.JsonData)
	if err != nil {
		return nil, err
	}
	resp = &base.BasePostResp{
		Code: "201",
		Msg:  "ok",
	}
	return
}

// CreateProductionPlanReport implements the PlanServiceImpl interface.
func (s *PlanServiceImpl) CreateProductionPlanReport(ctx context.Context, req *base.BasePostReq) (resp *base.BasePostResp, err error) {
	_, err = createForKey(req.GetId(), req.JsonData)
	if err != nil {
		return nil, err
	}
	resp = &base.BasePostResp{
		Code: "201",
		Msg:  "ok",
	}
	return
}

// UpdateProductionPlan implements the PlanServiceImpl interface.
func (s *PlanServiceImpl) UpdateProductionPlan(ctx context.Context, req *base.BasePutReq) (resp *base.BasePutResp, err error) {
	_, err = updateForKey(req.GetId(), req.JsonData)
	if err != nil {
		return nil, err
	}
	resp = &base.BasePutResp{
		Code: "200",
		Msg:  "ok",
	}
	return
}

// UpdateProductionPlanReport implements the PlanServiceImpl interface.
func (s *PlanServiceImpl) UpdateProductionPlanReport(ctx context.Context, req *base.BasePutReq) (resp *base.BasePutResp, err error) {
	_, err = updateForKey(req.GetId(), req.JsonData)
	if err != nil {
		return nil, err
	}
	resp = &base.BasePutResp{
		Code: "200",
		Msg:  "ok",
	}
	return
}

// DeleteProductionPlan implements the PlanServiceImpl interface.
func (s *PlanServiceImpl) DeleteProductionPlan(ctx context.Context, req *base.BaseDeleteReq) (resp *base.BaseDeleteResp, err error) {
	_, err = deleteForKey(req.GetId())
	if err != nil {
		return nil, err
	}
	resp = &base.BaseDeleteResp{
		Code: "200",
		Msg:  "ok",
	}
	return
}

// DeleteProductionPlanReport implements the PlanServiceImpl interface.
func (s *PlanServiceImpl) DeleteProductionPlanReport(ctx context.Context, req *base.BaseDeleteReq) (resp *base.BaseDeleteResp, err error) {
	_, err = deleteForKey(req.GetId())
	if err != nil {
		return nil, err
	}
	resp = &base.BaseDeleteResp{
		Code: "200",
		Msg:  "ok",
	}
	return
}

func readForKey(formID string) ([]byte, error) {
	return PlanOrg.EvaluateTransaction("ReadForm", formID)
}
func readHistoryForKey(formID string) ([]byte, error) {
	return PlanOrg.EvaluateTransaction("ReadFormHistoryForKey", formID)
}
func readForRange(formIDStart string, formIDEnd string) ([]byte, error) {
	return PlanOrg.EvaluateTransaction("ReadFormsByRange", formIDStart, formIDEnd)
}
func createForKey(formID string, formJSON string) ([]byte, error) {
	return PlanOrg.SubmitTransaction("CreateForm", formID, formJSON)
}
func updateForKey(formID string, formJSON string) ([]byte, error) {
	return PlanOrg.SubmitTransaction("UpdateForm", formID, formJSON)
}
func deleteForKey(formID string) ([]byte, error) {
	return PlanOrg.SubmitTransaction("DeleteForm", formID)
}
